package com.glory.dgcpcpda.activity.mfg;


import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.adapter.EdcSetBinAdapter;
import com.glory.dgcpcpda.adapter.FTLowQualitySamplingAdapter;
import com.glory.dgcpcpda.adapter.LastBatchAdapter;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.entity.EdcEntity;
import com.glory.dgcpcpda.entity.EdcSetBinEntity;
import com.glory.dgcpcpda.entity.EqpInfoEntity;
import com.glory.dgcpcpda.entity.EquipMentEntity;
import com.glory.dgcpcpda.entity.EywEqpWorkEntity;
import com.glory.dgcpcpda.entity.FTLowQualitySamplingEntity;
import com.glory.dgcpcpda.entity.LotInfoEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.LotUtils;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class FTLowQualitySamplingActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.etGndNum)
    FreeUI_ClearEditText etGndNum;
    @BindView(R.id.etTestspe)
    FreeUI_ClearEditText etTestspe;
    @BindView(R.id.rvLastBatchList)
    RecyclerView rvLastBatchList;
    @BindView(R.id.llMain)
    LinearLayout llMain;

    private FTLowQualitySamplingAdapter ftLowQualitySamplingAdapter;
    String[] planets = {"项目","测试规格","样品1","样品2","样品3","样品4","样品5","样品6","样品7","样品8","样品9","样品10"};


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_ft_low_quality_sampling;
    }

    @Override
    protected void initView() {
        initRecyclerView(rvLastBatchList,this);
        tvTitle.setText("测试低良取样");
        setMainLayout(llMain);
        ftLowQualitySamplingAdapter = new FTLowQualitySamplingAdapter(null);
        ftLowQualitySamplingAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        ftLowQualitySamplingAdapter.isFirstOnly(false);
        rvLastBatchList.setAdapter(ftLowQualitySamplingAdapter);

        List<FTLowQualitySamplingEntity> edcsetlist= new ArrayList<>();
        for(String item : planets){
            FTLowQualitySamplingEntity edcset= new FTLowQualitySamplingEntity();
            edcset.setNAME(item);
            edcsetlist.add(edcset);
        }
        ftLowQualitySamplingAdapter.setNewData(edcsetlist);
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.tvBaseSubmit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                if(getText(etLot).equals("")){
                    showToast("请输入批次号",false);
                    return;
                }
                List<FTLowQualitySamplingEntity.LowQuality> lowQualityEntityList= new ArrayList<>();//提交的List
                FTLowQualitySamplingEntity.LowQuality lowQualityEntity = new FTLowQualitySamplingEntity.LowQuality();
                lowQualityEntity.settype(1);
                lowQualityEntity.settypeName("O/S不良项目（I）");
                FTLowQualitySamplingEntity.LowQuality lowQualityEntity2 = new FTLowQualitySamplingEntity.LowQuality();
                lowQualityEntity2.settype(2);
                lowQualityEntity2.settypeName("F/T不良项目（II）");
                FTLowQualitySamplingEntity.LowQuality lowQualityEntity3 = new FTLowQualitySamplingEntity.LowQuality();
                lowQualityEntity3.settype(3);
                lowQualityEntity3.settypeName("F/T不良项目（III）");
                for (FTLowQualitySamplingEntity entity: ftLowQualitySamplingAdapter.getData()){
                    if(entity.getNAME().equals("项目")){
                        lowQualityEntity.setproject(entity.getOSUNHEALTHY());
                        lowQualityEntity2.setproject(entity.getFT1UNHEALTHY());
                        lowQualityEntity3.setproject(entity.getFT2UNHEALTHY());
                    }
                    if(entity.getNAME().equals("测试规格")){
                        lowQualityEntity.settestSpec(entity.getOSUNHEALTHY());
                        lowQualityEntity2.settestSpec(entity.getFT1UNHEALTHY());
                        lowQualityEntity3.settestSpec(entity.getFT2UNHEALTHY());
                    }
                    if(entity.getNAME().equals("样品1")){
                        lowQualityEntity.setvalue1(entity.getOSUNHEALTHY());
                        lowQualityEntity2.setvalue1(entity.getFT1UNHEALTHY());
                        lowQualityEntity3.setvalue1(entity.getFT2UNHEALTHY());
                    }
                    if(entity.getNAME().equals("样品2")){
                        lowQualityEntity.setvalue2(entity.getOSUNHEALTHY());
                        lowQualityEntity2.setvalue2(entity.getFT1UNHEALTHY());
                        lowQualityEntity3.setvalue2(entity.getFT2UNHEALTHY());
                    }
                    if(entity.getNAME().equals("样品3")){
                        lowQualityEntity.setvalue3(entity.getOSUNHEALTHY());
                        lowQualityEntity2.setvalue3(entity.getFT1UNHEALTHY());
                        lowQualityEntity3.setvalue3(entity.getFT2UNHEALTHY());
                    }
                    if(entity.getNAME().equals("样品4")){
                        lowQualityEntity.setvalue4(entity.getOSUNHEALTHY());
                        lowQualityEntity2.setvalue4(entity.getFT1UNHEALTHY());
                        lowQualityEntity3.setvalue4(entity.getFT2UNHEALTHY());
                    }
                    if(entity.getNAME().equals("样品5")){
                        lowQualityEntity.setvalue5(entity.getOSUNHEALTHY());
                        lowQualityEntity2.setvalue5(entity.getFT1UNHEALTHY());
                        lowQualityEntity3.setvalue5(entity.getFT2UNHEALTHY());
                    }
                    if(entity.getNAME().equals("样品6")){
                        lowQualityEntity.setvalue6(entity.getOSUNHEALTHY());
                        lowQualityEntity2.setvalue6(entity.getFT1UNHEALTHY());
                        lowQualityEntity3.setvalue6(entity.getFT2UNHEALTHY());
                    }
                    if(entity.getNAME().equals("样品7")){
                        lowQualityEntity.setvalue7(entity.getOSUNHEALTHY());
                        lowQualityEntity2.setvalue7(entity.getFT1UNHEALTHY());
                        lowQualityEntity3.setvalue7(entity.getFT2UNHEALTHY());
                    }
                    if(entity.getNAME().equals("样品8")){
                        lowQualityEntity.setvalue8(entity.getOSUNHEALTHY());
                        lowQualityEntity2.setvalue8(entity.getFT1UNHEALTHY());
                        lowQualityEntity3.setvalue8(entity.getFT2UNHEALTHY());
                    }
                    if(entity.getNAME().equals("样品9")){
                        lowQualityEntity.setvalue9(entity.getOSUNHEALTHY());
                        lowQualityEntity2.setvalue9(entity.getFT1UNHEALTHY());
                        lowQualityEntity3.setvalue9(entity.getFT2UNHEALTHY());
                    }
                    if(entity.getNAME().equals("样品10")){
                        lowQualityEntity.setvalue10(entity.getOSUNHEALTHY());
                        lowQualityEntity2.setvalue10(entity.getFT1UNHEALTHY());
                        lowQualityEntity3.setvalue10(entity.getFT2UNHEALTHY());
                    }
                }

                lowQualityEntityList.add(lowQualityEntity);
                lowQualityEntityList.add(lowQualityEntity2);
                lowQualityEntityList.add(lowQualityEntity3);

                FreeUI_EntityCallBack<BaseResponseBean<LotInfoEntity>> callback = new FreeUI_DialogEntityCallBack<BaseResponseBean<LotInfoEntity>>
                        (new TypeToken<BaseResponseBean<LotInfoEntity>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<LotInfoEntity>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                            showConfirmDialog(FTLowQualitySamplingActivity.this,"提交成功");
                        }
                    }
                    @Override
                    public void onError(Response<BaseResponseBean<LotInfoEntity>> response) {
                        super.onError(response);
                    }
                };
                JsonObject map = new JsonObject();
                map.addProperty("assembleBatch", getText(etLot));
                map.addProperty("GND", getText(etGndNum));
                map.add("items",new JsonParser().parse(new Gson().toJson(lowQualityEntityList)).getAsJsonArray());
                JsonObject qmsBodyWrapper = new JsonObject();
                qmsBodyWrapper.add("QMSBODY", map);
                NetUtils.requestPostNet("CPC.CPCQMSTESTFA", "TRANSFERMES",this, qmsBodyWrapper, callback);
                break;
        }
    }


}